﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OleDb;
using XDesigner.Writer;
using XDesigner.Writer.Data;
using XDesigner.Writer.Dom;

namespace XDesigner.Writer.Test
{
    /// <summary>
    /// 应用程序数据提供者
    /// </summary>
    public class MyDataProvider : IInputFieldListItemProvider 
    {
        private static MyDataProvider _Instance = null;
        /// <summary>
        /// 对象唯一静态实例
        /// </summary>
        public static MyDataProvider Instance
        {
            get
            {
                if (_Instance == null)
                {
                    _Instance = new MyDataProvider();
                }
                return _Instance;
            }
        }

        /// <summary>
        /// 创建数据库连接对象
        /// </summary>
        /// <returns></returns>
        public IDbConnection CreateConnection()
        {
            OleDbConnection conn = new OleDbConnection();
            conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\"" + System.IO.Path.Combine( System.Windows.Forms.Application.StartupPath , "demo.mdb") + "\"";
            return conn;
        }



        System.Collections.IEnumerable IInputFieldListItemProvider.GetListItems(string name)
        {
            List<InputFieldListItem> result = new List<InputFieldListItem>();
            string strSQL = null;
            switch (name)
            {
                case "COUNTRY":
                    strSQL = "Select SysDesc , SysDesc From SysCode Where SysType='0003' Order by SysDesc";
                    break;
                case "NATION":
                    strSQL = "Select SysDesc , SysDesc From SysCode Where SysType='0017' Order By SysDesc";
                    break;
                case "EXAMIN" :
                    strSQL = "Select Examine , ECode From Examine Order By Examine";
                    break;
                case "DOCEX":
                    strSQL = "Select Name , Medecode From Docex Order By Name";
                    break;
            }
            if (strSQL != null)
            {
                using (IDbConnection conn = CreateConnection())
                {
                    conn.Open();
                    using (IDbCommand cmd = conn.CreateCommand())
                    {
                        cmd.CommandText = strSQL;
                        IDataReader reader = cmd.ExecuteReader();
                        while (reader.Read())
                        {
                            InputFieldListItem item = new InputFieldListItem();
                            item.Text = Convert.ToString(reader.GetValue(0));
                            item.Value = Convert.ToString(reader.GetValue(1));
                            result.Add(item);
                        }
                        reader.Close();
                    }//using
                }//using
            }
            return result;
        }
    }
}
